package org.mspring.mlog.service;

import org.mspring.mlog.entity.Post;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.search.engine.LuceneSearchEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 文章搜索服务
 *
 * @since 2014年5月4日
 * @author Gao Youbo
 */
@Service
public class PostSearchService {
    @Autowired
    private LuceneSearchEngine luceneSearchEngine;
    @Autowired
    private PostService postService;

    public void reindexAll() throws Exception {
        int batch = 100;
        int page = 1;
        while (true) {
            QueryBuilder queryBuilder = QueryBuilder.instance(Post.class).buildEqual("status", Post.Status.PUBLISH);
            Page<Post> postPage = new Page<>();
            postPage.setPageNo(page);
            postPage.setPageSize(batch);
            postPage = postService.listPage(queryBuilder, postPage);
            luceneSearchEngine.doIndex(postPage.getResult());
            if (postPage.getResult().size() < batch) {
                break;
            }
            page++;
        }
    }
}
